package io.renren.modules.nuaa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.modules.nuaa.dao.TSignupRecordDao;
import io.renren.modules.nuaa.dto.TSignupRecordDTO;
import io.renren.modules.nuaa.entity.TSignupRecordEntity;
import io.renren.modules.nuaa.service.TSignupRecordService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 报名/申请记录
 *
 * @author whc 15953295779@126.com
 * @since 3.0 2023-10-31
 */
@Service
public class TSignupRecordServiceImpl extends CrudServiceImpl<TSignupRecordDao, TSignupRecordEntity, TSignupRecordDTO> implements TSignupRecordService {

    @Override
    public QueryWrapper<TSignupRecordEntity> getWrapper(Map<String, Object> params){
        QueryWrapper<TSignupRecordEntity> wrapper = new QueryWrapper<>();
        String categoryIdStr = (String)params.get("categoryId");
        String categoryIdIdStr = (String)params.get("categoryIdId");
        String guardian = (String) params.get("guardian");
        String school = (String) params.get("school");
        String phone = (String) params.get("phone");
        String idCard = (String) params.get("idCard");
        String name = (String) params.get("name");


        if(StringUtils.isNotBlank(categoryIdStr)) {
            Integer categoryId = Integer.parseInt(categoryIdStr);
            if (categoryId.intValue() == 6) {
                wrapper.eq("category_id", 6);
            } else if (categoryId.intValue() == 4) {
                wrapper.and(w -> {
                    w.eq("category_id", 4).or().eq("category_id", 5);
                });
            }
        }

        if(StringUtils.isNotBlank(categoryIdIdStr)){
            Integer categoryIdId = Integer.parseInt(categoryIdIdStr);
            wrapper.eq("category_id", categoryIdId);

        }
        if(StringUtils.isNotBlank(guardian)){
            wrapper.like("guardian",guardian);
        }
        if(StringUtils.isNotBlank(school)){
            wrapper.like("school",school);
        }
        if(StringUtils.isNotBlank(name)){
            wrapper.like("user_name",name);
        }
        if(StringUtils.isNotBlank(phone)){
            wrapper.eq("phone",phone);
        }
        if(StringUtils.isNotBlank(idCard)){
            wrapper.eq("id_card",idCard);
        }



        return wrapper;
    }

    @Override
    public TSignupRecordEntity queryIfExist(String userName, String idCard) {
        QueryWrapper<TSignupRecordEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name",userName);
        wrapper.eq("id_card",idCard);
        wrapper.last("limit 1");
        return baseDao.selectOne(wrapper);
    }


    @Override
    public TSignupRecordEntity queryIfExist(TSignupRecordDTO dto) {
        QueryWrapper<TSignupRecordEntity> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(dto.getSchool())) {
            wrapper.eq("school", dto.getSchool());
        }
        if (dto.getCategoryId() != null) {
            wrapper.eq("category_id", dto.getCategoryId());
        }
        wrapper.last("limit 1");

        TSignupRecordEntity entity = baseDao.selectOne(wrapper);

        //todo 邀请码验证

        return entity;
    }
}